package io.geekidea.boot.viptype.controller;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.geekidea.boot.common.PageDto;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.viptype.dto.ViptypeDto;
import io.geekidea.boot.viptype.entity.Viptype;
import io.geekidea.boot.viptype.query.ViptypeQuery;
import io.geekidea.boot.viptype.service.ViptypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.math3.ml.neuralnet.UpdateAction;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/viptype")
@Tag(name = "会员套餐")
public class ViptypeController {

    @Resource
    private ViptypeService viptypeService;

    @PostMapping("/search")
    @Operation(summary = "查询会员套餐所有数据",description = "按照条件组合分页查询会员套餐数据列表")
    public ApiResult viptypeList(@RequestBody ViptypeQuery viptypeQuery){
        Page<Viptype> page=new Page<>(viptypeQuery.getPage(),viptypeQuery.getSize());
        LambdaQueryWrapper<Viptype> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(viptypeQuery.getVipType()),Viptype::getVipType,viptypeQuery.getVipType());
        if (viptypeQuery.getStatus() != null) {
            if (viptypeQuery.getStatus().equals("true")) {
                viptypeQuery.setStatus("0");
            }else if (viptypeQuery.getStatus().equals("false")) {
                viptypeQuery.setStatus("1");
            }
        }
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(viptypeQuery.getStatus()),Viptype::getStatus,viptypeQuery.getStatus());
        Page<Viptype> pageDto=viptypeService.page(page,lambdaQueryWrapper);
        PageDto<Viptype> pageInfo=new PageDto<>(pageDto.getRecords(),pageDto.getTotal(),pageDto.getSize(),pageDto.getCurrent());
        return ApiResult.success(pageInfo);
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "根据ID查询单条数据")
    public ApiResult getViptypeById(@PathVariable(value = "id")Integer id){
        final Viptype viptype=viptypeService.getById(id);
        return ApiResult.success(viptype);
    }

    @DeleteMapping("/romove/{id}")
    @Operation(summary = "逻辑删除战区")
    public ApiResult removeViptype(@PathVariable(value = "id")Integer id){
        viptypeService.removeById(id);
        return ApiResult.success("删除会员类型成功！");
    }

    @PostMapping("/add")
    public ApiResult addViptype(@RequestBody ViptypeDto viptypeDto){
        Viptype viptypeEntity=new Viptype();
        if (viptypeDto.getStatus().equals("true")) {
            viptypeDto.setStatus("0");
        }else if (viptypeDto.getStatus().equals("false")) {
            viptypeDto.setStatus("1");
        }
        BeanUtil.copyProperties(viptypeDto,viptypeEntity);
        viptypeService.save(viptypeEntity);
        return ApiResult.success("添加会员套餐类型成功！");
    }

    //逻辑删除
    @GetMapping("/del/{id}")
    @Operation(summary = "物理删除",description = "根据id逻辑删除对应的列")
    public ApiResult del(@PathVariable Integer id){
        boolean update = viptypeService.delById(id);
        if (!update) {
            return ApiResult.success("删除失败");
        }
        return ApiResult.success("删除成功");

    }


    @PostMapping("/update")
    public ApiResult updateArea(@Validated(value = {UpdateAction.class})@RequestBody ViptypeDto viptypeDto){
        Viptype viptypeEntity=new Viptype();
        if (viptypeDto.getStatus().equals("true")) {
            viptypeDto.setStatus("0");
        }else if (viptypeDto.getStatus().equals("false")) {
            viptypeDto.setStatus("1");
        }
        BeanUtil.copyProperties(viptypeDto,viptypeEntity);
        viptypeService.updateById(viptypeEntity);
        return ApiResult.success("更改战区数据成功！");
    }


}
